package com.kisszpy.system.actions;

import java.util.Iterator;
import java.util.List;

import net.sf.json.JSONObject;

import com.kisszpy.core.BaseAction;
import com.kisszpy.core.utils.CaptchaCode;
import com.kisszpy.core.utils.PageModel;
import com.kisszpy.core.utils.TreeNode;
import com.kisszpy.system.entity.SysMenu;
import com.kisszpy.system.entity.SysRole;
import com.kisszpy.system.entity.SysUser;
import com.kisszpy.system.service.SysMenuService;
import com.kisszpy.system.service.SysUserService;

public class SysUserAction extends BaseAction{

	private static final long serialVersionUID = 4398561091945690778L;

	private SysUserService sysUserService;
	
	private SysMenuService sysMenuService;
	
	public void setSysMenuService(SysMenuService sysMenuService) {
		this.sysMenuService = sysMenuService;
	}
	private SysUser sysUser = new SysUser();
	// 验证码
	private String code;
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public SysUser getSysUser() {
		return sysUser;
	}
	public void setSysUser(SysUser sysUser) {
		this.sysUser = sysUser;
	}
	public void setSysUserService(SysUserService sysUserService) {
		this.sysUserService = sysUserService;
	}
	// 获取所有用户
	public String getAllUser(){
		PageModel<SysUser> pm = this.sysUserService.getAll(this.getPageNow(),this.getPageSize());
		JSONObject json = JSONObject.fromObject(pm);
		renderJSON(json.toString());
		return null;
	}
	// 系统登录
	public String login() throws Exception {
		SysUser user = this.sysUserService.login(sysUser);
		if(null!=user){
			//if(this.code.equalsIgnoreCase(session.get("code").toString())){ // 验证码校验
				session.put("login_key", user);
				return "login_success";
			//}
		}
		return "login_ui";
	}
	/*JsonConfig config = new JsonConfig();
	config.setJsonPropertyFilter(new PropertyFilter() {
		public boolean apply(Object arg0, String arg1, Object arg2) {
			if("roleSet".equals(arg1)||"menuSet".equals(arg1)){
				return true;
			}
			return false;
		}
	});*/
	// 载入用户信息
	public String loadLoginInfo(){
		SysUser user = (SysUser) session.get("login_key");
		JSONObject json = JSONObject.fromObject(user);
		renderJSON(json.toString());
		return null;
	};
	// 渲染树形结构
	public String showTree() throws Exception {
		SysUser user = (SysUser) session.get("login_key");
		Iterator<SysRole> it = user.getRoleSet().iterator();
		Integer[] ids = new Integer[user.getRoleSet().size()];
		int i = 0;
		while(it.hasNext()){
			Integer id = it.next().getId();
			ids[i] = id;
			i++;
		}
		List<SysMenu> list = this.sysMenuService.getMenus(ids);
		TreeNode root = new TreeNode();
		root.setName("系统功能菜单");
		root.setId(-1);
		this.buildTree(list, 0, root );
		JSONObject json = JSONObject.fromObject(root);
		System.out.println(json.toString());
		renderJSON(json.toString());
		return null;
	}
	// 递归方式进行树形结构的构建和对象模型映射
	private void buildTree(List<SysMenu> list,Integer id,TreeNode root){
		for(int i = 0;i<list.size();i++){
			if(list.get(i).getPid().equals(id)){
				// 映射树形结构
				TreeNode node = new TreeNode();
				SysMenu menu = list.get(i);
				node.setId(menu.getId());
				node.setName(menu.getName());
				node.setUrl(menu.getAction()==null?"#":menu.getAction().getActionUrl());
				node.setTarget("main");
				root.getNodes().add(node);
				buildTree(list, node.getId(), node);
			}
		}
	}
	// 用户注销
	public String logout() throws Exception {
		session.remove("login_key");
		response.getWriter().print("<script>window.top.location.href='./login.html'</script>");
		return null;
	}
	// 验证码渲染
	public String renderCode() throws Exception {
		System.out.println(this.sysUserService);
		CaptchaCode.build(request, response);
		return null;
	}
}
